/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package packageVotaciones;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.servlet.ServletException;

/**
 *
 * @author Manuel Vinuesa
 */
public class Censo {
    private String nif;
    private String pass;
    private String voto;

    public Censo() {
    }

    public Censo(String nif, String voto) {
        this.nif = nif;
        this.voto = voto;
    }

    public String getNif() {
        return nif;
    }

    public void setNif(String nif) {
        this.nif = nif;
    }

    public String getVoto() {
        return voto;
    }

    public void setVoto(String voto) {
        this.voto = voto;
    }
    @Override
    public String toString() {
        return "Censo{" + "nif=" + nif + ", voto=" + voto + '}';
    }
    public ArrayList<Censo> getArrayCenso(Connection _conexion){
        ArrayList<Censo> ArrayCenso=new ArrayList();
        try{
                Statement stmt = _conexion.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT NIF,VOTO "
                        + "FROM votantes");
                while(rs.next()){
                    setNif(rs.getString("NIF"));
                    setVoto(rs.getString("VOTO"));
                    Censo OCenso=new Censo(getNif(), getVoto());
                    ArrayCenso.add(OCenso);
                }
               
            }catch(SQLException e){
                System.out.println("SQL Exception: "+e.toString());
            }
        return ArrayCenso;
    }
    
    public boolean comprVotante(String _nif, String _pass, Connection _conexion) throws SQLException{
        try{
                Statement stmt = _conexion.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT NIF FROM votantes WHERE NIF='"+_nif+"' AND PASS='"+_pass+"' AND VOTO='N'");
                if(rs.next()){
                    return true;
                }
            }catch(SQLException e){
                System.out.println("SQL Exception: "+e.toString());
            }
        return false;
    }
    public void votar(String _nif, Connection _conexion) throws SQLException{
        int resultado=0;
        try{
                Statement stmt = _conexion.createStatement();
                resultado = stmt.executeUpdate("UPDATE votantes SET VOTO='S' WHERE NIF='"+_nif+"'");
                
            }catch(SQLException e){
                System.out.println("SQL Exception: "+e.toString());
            }
        if(resultado==0){
            throw new SQLException();
        }
    }
    }
